﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Sound;

namespace Darwin.Source.Components.World.Entities.MinionTypes
{
    class CSimpleMonster : CEntity
    {
        const float Height = 1.82f;
        const float MaxMoveSpeed = 0.1f;

        const float AttackRange = 3.0f;

        const float visibility = 45.0f;

        CEntity target = null;

        public override bool Initialise( Team.CTeam myTeam )
        {
            return base.Initialise(myTeam);
        }

        public override void DoUpdate(Microsoft.Xna.Framework.GameTime gameTime, World w, List<PotentialFeild.CPotentialFeildVector> potentialFeildVectors, Vector4[] gay)
        {
            if (target == null)
            {
                foreach (CEntity it_entities in w.m_entityDictionary.Values)
                {
                    if (it_entities.teamSide != this.teamSide)
                    {
                        if ( Vector3.Distance(it_entities.Position , this.Position) < visibility )
                            target = it_entities;
                    }
                }
            }
            else
            {
                if (!w.m_entityDictionary.ContainsValue(target))    // Kill by someone else
                {
                    target = null;
                    return;
                }

                Vector3 direction = target.Position - this.Position;
                direction.Normalize();
                this.Position += direction * MaxMoveSpeed;

                if (Vector3.Distance(this.Position, target.Position) < AttackRange)
                {
                    SoundManager.getSoundManager().playSound(2);
                    target.EntityState = eEntityState._DEAD;
                }
            }

        }

        public override void DoRender()
        {
#if DEBUG_GRAPHICS_ENABLED
            //
            //  Given a valid XZ value the terrain will return to you a height at that point, and the normal of the triangle you are on
            //
            Vector3 v3Normal;
            m_v3Position.Y = Singleton<SunXNA.Terrain.CTerrain>.Instance.GetHeightAtXZ(m_v3Position, out v3Normal);

            Singleton<SunXNA.DebugGraphics.CManager>.Instance.Add3DObject(m_UID,
                                                                          SunXNA.DebugGraphics.Type.Box,
                                                                          Color.Red,
                                                                          m_v3Position + (v3Normal * (Height * 0.5f)),
                                                                          v3Normal,
                                                                          new Vector3(0.7f, 0.7f, Height),
                                                                          1.0f);
#endif
        }
    }
}
